<?php
namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Cookie;
use app\model\User;
use app\model\Cash;

Class Userdo extends Controller{

	// 注册用户
	public function Register(){
		$pars = input('post.');
		$user = User::get(['user_name'=>input('user_name'),
							'user_email'=>input('user_email')]);
		if(null == $user){
			$pars['user_id'] = getId();
			$pars['user_password'] = md5($pars['user_password']);
			$user = User::create($pars);
			return getResponse(0,$user);
		}else{
			return getResponse(1,"该用户名或邮箱已存在!");
		}
	}

	// 验证用户登录
	public function Login(){
		$pars['user_email'] = input('user_email');
		$pars['user_password'] = md5(input('user_password'));
		$user = User::get($pars);
		if(null == $user){
			return getResponse(1,"用户名或密码错误");
		}
		if(isset($_POST["rm"])){
			Cookie::prefix('front');
			Cookie::set("user_id",$user['user_id'],['prefix'=>'front','expire'=>3600*24*30]);
			Cookie::set("user_password",$user['user_password'],['prefix'=>'front','expire'=>3600*24*30]);
			Cookie::set("user_name",$user['user_name'],['prefix'=>'front','expire'=>3600*24*30]);
		}
		session::set("user_name",$user['user_name']);
		session::set("user_password",$user['user_password']);
		session::set("user_id",$user['user_id']);
		session::set("user_balance",$user['user_balance']);
		return getResponse(0,$user);
	}

	// 用户登出
	public function Logout(){
		Cookie::clear("front");
		session::clear();
		return $this->redirect("index/login");
	}

	// 获取用户信息
	public function getuserInfo(){
		$user = User::get(input("user_id"));
		return getResponse(0,$user);
	}

	// 用户充值
	public function Recharge(){
		$user = User::get(Session::get("user_id"));
		if(null == $user){
			return getResponse(1,"充值失败!");
		}else{
			$user->user_balance += input('recharge');
			$user = $user->save();
			return getResponse(0,$user);
		}
	}

	// 用户提现
	public function applyCash(){
		$pars = input('post.');
		$pars['cash_id'] = getId();
		$pars['cash_num'] = input('cash_num');
		$pars['user_id'] = Session::get("user_id");
		$cash = Cash::create($pars);
		$user = User::get(Session::get("user_id"));
		if(null == $cash || null == $user){
			return getResponse(1,"提现请求失败!");
		}else{
			$user->user_balance -= $pars['cash_num'];
			$user->save();
			return getResponse(0,$user);
		}
	}

	// 修改用户
	public function editUser(){
		$user = User::get(Session::get("user_id"));
		if(null == $user){
			return getResponse(1,"提现请求失败!");
		}else{
			$user->user_phone = input('user_phone');
			$user->user_email = input('user_email');
			$user->save();
			return getResponse(0,$user);
		}
	}

	// 获取用户待审核提现列表
	public function getUnauditedCashList(){
		$pars = input('post.');
		$pars['is_pass'] = 0;
		$pars['is_delete'] = 0;
		$list = Cash::all($pars);
		if(null == $list){
			return getResponse(1,"数据为空!");
		}else{
			return getResponse(0,$list);
		}
	}

	// 获取用户已审核提现列表
	public function getAuditedCashList(){
		$pars = input('post.');
		$pars['is_pass'] = 1;
		$pars['is_delete'] = 0;
		$list = Cash::all($pars);
		if(null == $list){
			return getResponse(1,"数据为空!");
		}else{
			return getResponse(0,$list);
		}
	}

	// 撤销申请
	public function removeCash(){
		$pars = input('post.');
		$cash = Cash::get($pars);
		if(null == $cash){
			return getResponse(1,"请求失败!");
		}else{
			$cash->is_delete = 1;
			$cash->save();
			return getResponse(0,$cash);
		}
	}

	// 验证用户
	public function checkUser(){
		$pars['user_email'] = input('user_email');
		$pars['user_phone'] = input('user_phone');
		$user = User::get($pars);
		if(null == $user){
			return getResponse(1,"验证失败!");
		}
		return getResponse(0,$user);
	}

	// 重置密码
	public function resetPass(){
		$user = User::get(input('user_id'));
		if(null == $user){
			return getResponse(1,"修改失败!");
		}
		$user->user_password = md5(input('user_password'));
		$user->save();
		return getResponse(0,$user);
	}

} 

 ?>